package common.library.utils;

import android.app.Activity;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.IdRes;
import androidx.appcompat.app.AppCompatActivity;
import common.library.models.Callback;
import common.library.models.Size;
import lombok.var;

public class ViewUtil {

    public static View getById(Activity activity, @IdRes int id) {
        return activity.findViewById(id);
    }

    public static void addClickListener(View view, Callback callback) {
        view.setOnClickListener(v -> callback.run());
    }

    public static void addClickListener(Activity activity, @IdRes int id, View.OnClickListener callback) {
        activity.findViewById(id).setOnClickListener(callback);
    }

    public static void addClickListener(Activity activity, @IdRes int id, Callback callback) {
        addClickListener(activity, id, view -> callback.run());
    }

    public static void clickToQuitApplication(Activity activity, @IdRes int id) {
        addClickListener(activity, id, view -> ApplicationUtil.close());
    }

    public static void Hide(View view) {
        view.setVisibility(View.GONE);
    }

    public static void Hide(final AppCompatActivity activity, final int id) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                View view = activity.findViewById(id);
                view.setVisibility(View.GONE);
            }
        });
    }

    public static void Show(View view) {
        view.setVisibility(View.VISIBLE);
    }

    public static void Show(final AppCompatActivity activity, final int id) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                View view = activity.findViewById(id);
                view.setVisibility(View.VISIBLE);
            }
        });
    }

    public static void setLayout(View view, Size size) {
        var params = view.getLayoutParams();
        LogUtil.info("change size ({},{}) to ({},{})", params.width, params.height, size.width, size.height);
        params.width = size.width;
        params.height = size.height;
        view.setLayoutParams(params);
    }

    public static void setLayout(View view, int width, int height) {
        var size = new Size(width, height);
        var params = view.getLayoutParams();
        LogUtil.info("change size ({},{}) to ({},{})", params.width, params.height, size.width, size.height);
        params.width = size.width;
        params.height = size.height;
        view.setLayoutParams(params);
    }

    /**
     * 同时适用EditText
     */
    public static void setText(AppCompatActivity activity, final TextView textView, final String message) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                textView.setVisibility(View.VISIBLE);
                textView.setText(message);
            }
        });
    }

    public static void setText(final AppCompatActivity activity, final int id, final String message) {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                TextView textView = activity.findViewById(id);
                textView.setVisibility(View.VISIBLE);
                textView.setText(message);
            }
        });
    }
}
